from sympy import symbols, sqrt, diff, acos


x1, y1, z1, x2, y2, z2 = symbols('x1 y1 z1 x2 y2 z2')

r1 = sqrt(x1*x1 + y1*y1 + z1*z1)
r2 = sqrt(x2*x2 + y2*y2 + z2*z2)

cosA = (x1*x2+y1*y2+z1*z2)/r1/r2

symbols = {r1:'r1', r2:'r2', cosA:'cosA', (x1*x2 + y1*y2 + z1*z2):'cosA*r1*r2'}

print "dx[0].x =", diff(cosA, x1).subs(symbols)
print "dx[0].y =", diff(cosA, y1).subs(symbols)
print "dx[0].z =", diff(cosA, z1).subs(symbols)

print "dx[1].x =", diff(cosA, x2).subs(symbols)
print "dx[1].y =", diff(cosA, y2).subs(symbols)
print "dx[1].z =", diff(cosA, z2).subs(symbols)

print "dx2[0][0][0] =", cosA.diff(x1, x1).subs(symbols)
print "dx2[0][0][1] =", cosA.diff(x1, y1).subs(symbols)
print "dx2[0][0][2] =", cosA.diff(x1, z1).subs(symbols)
print "dx2[0][1][0] =", cosA.diff(y1, x1).subs(symbols)
print "dx2[0][1][1] =", cosA.diff(y1, y1).subs(symbols)
print "dx2[0][1][2] =", cosA.diff(y1, z1).subs(symbols)
print "dx2[0][2][0] =", cosA.diff(z1, x1).subs(symbols)
print "dx2[0][2][1] =", cosA.diff(z1, y1).subs(symbols)
print "dx2[0][2][2] =", cosA.diff(z1, z1).subs(symbols)

print "dx2[1][0][0] = dx2[2][0][0] = ", cosA.diff(x1, x2).subs(symbols)
print "dx2[1][0][1] = dx2[2][1][0] = ", cosA.diff(x1, y2).subs(symbols)
print "dx2[1][0][2] = dx2[2][2][0] = ", cosA.diff(x1, z2).subs(symbols)
print "dx2[1][1][0] = dx2[2][0][1] = ", cosA.diff(y1, x2).subs(symbols)
print "dx2[1][1][1] = dx2[2][1][1] = ", cosA.diff(y1, y2).subs(symbols)
print "dx2[1][1][2] = dx2[2][2][1] = ", cosA.diff(y1, z2).subs(symbols)
print "dx2[1][2][0] = dx2[2][0][2] = ", cosA.diff(z1, x2).subs(symbols)
print "dx2[1][2][1] = dx2[2][1][2] = ", cosA.diff(z1, y2).subs(symbols)
print "dx2[1][2][2] = dx2[2][2][2] = ", cosA.diff(z1, z2).subs(symbols)


print "dx2[3][0][0] =", cosA.diff(x2, x2).subs(symbols)
print "dx2[3][0][1] =", cosA.diff(x2, y2).subs(symbols)
print "dx2[3][0][2] =", cosA.diff(x2, z2).subs(symbols)
print "dx2[3][1][0] =", cosA.diff(y2, x2).subs(symbols)
print "dx2[3][1][1] =", cosA.diff(y2, y2).subs(symbols)
print "dx2[3][1][2] =", cosA.diff(y2, z2).subs(symbols)
print "dx2[3][2][0] =", cosA.diff(z2, x2).subs(symbols)
print "dx2[3][2][1] =", cosA.diff(z2, y2).subs(symbols)
print "dx2[3][2][2] =", cosA.diff(z2, z2).subs(symbols)

